package com.hiyee.grace.result;

import java.util.Map;

/**
 * 统一的 api接口 返回格式定义类
 *
 * @author WangBin
 */
public class ResponseJsonResult {

    // 响应业务状态码
    private Integer status;

    // 响应消息
    private String msg;

    // 响应数据，可以是 Object、List、Map
    private Object data;

    /**
     * 构造方法（携带数据返回成功）
     *
     * @param data
     */
    public ResponseJsonResult(Object data) {
        this.status = ResponseStatusEnum.SUCCESS.getStatus();
        this.msg = ResponseStatusEnum.SUCCESS.getMsg();
        this.data = data;
    }

    /**
     * 构造方法（只返回状态码及消息）
     *
     */
    public ResponseJsonResult(ResponseStatusEnum responseStatusEnum) {
        this.status = responseStatusEnum.getStatus();
        this.msg = responseStatusEnum.getMsg();
    }

    /**
     * 构造方法（自定义状态码枚举返回成功）
     *
     */
    public ResponseJsonResult(ResponseStatusEnum responseStatusEnum, Object data) {
        this.status = responseStatusEnum.getStatus();
        this.msg = responseStatusEnum.getMsg();
        this.data = data;
    }

    /**
     * 构造方法（返回参数校验的 map）
     *
     * @param responseStatusEnum
     * @param map
     */
    public ResponseJsonResult(ResponseStatusEnum responseStatusEnum, Map<String, String> map) {
        this.status = responseStatusEnum.getStatus();
        String msg = "";
        for (String key : map.keySet()) {
            msg += map.get(key).concat(";");
        }
        this.msg = msg;
    }

    /**
     * 成功返回并携带数据
     *
     * @param data
     * @return
     */
    public static ResponseJsonResult success(Object data) {
        return new ResponseJsonResult(data);
    }

    /**
     * 成功返回不携带数据
     *
     * @return
     */
    public static ResponseJsonResult success() {
        return new ResponseJsonResult(ResponseStatusEnum.SUCCESS);
    }

    /**
     * 返回失败消息 （默认 500）
     *
     * @return
     */
    public static ResponseJsonResult fail() {
        return new ResponseJsonResult(ResponseStatusEnum.FAIL);
    }

    /**
     * 返回自定义错误消息
     *
     * @return
     */
    public static ResponseJsonResult error(ResponseStatusEnum responseStatusEnum) {
        return new ResponseJsonResult(responseStatusEnum);
    }

    /**
     * 返回异常消息
     *
     * @param responseStatusEnum
     * @return
     */
    public static ResponseJsonResult exception(ResponseStatusEnum responseStatusEnum) {
        return new ResponseJsonResult(responseStatusEnum);
    }

    /**
     * 返回参数校验 map
     *
     * @param map
     * @return
     */
    public static ResponseJsonResult errorMap(Map<String, String> map) {
        return new ResponseJsonResult(ResponseStatusEnum.FAIL, map);
    }

    /**
     * 返回自定义字符串
     *
     * @param map
     * @return
     */
    public static ResponseJsonResult error(String msg) {
        return new ResponseJsonResult(ResponseStatusEnum.FAIL, msg);
    }

    public ResponseJsonResult(){}

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
